package com.icinfo.cloud.provider.punish.controller;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.punish.dto.SimpleCaseSaveDto;
import com.icinfo.cloud.provider.punish.dto.SimpleCaseSaveFinishDto;
import com.icinfo.cloud.provider.punish.dto.SimpleCaseSaveOfAppDto;
import com.icinfo.cloud.provider.punish.service.ISimpleCaseApiService;
import com.icinfo.cloud.provider.punish.service.ISimpleCaseService;
import com.icinfo.cloud.provider.punish.vo.SimpleCaseDocVo;
import com.icinfo.cloud.provider.punish.vo.SimpleCaseSaveVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 简易处罚操作类 提供外部API
 *
 * @Author zhengqiang
 * @Date 2022/7/18 14:22
 **/
@RestController
@RequestMapping("/app/simpleCase/api")
@Slf4j
public class SimpleCaseApiController extends BaseController {

    @Resource
    private ISimpleCaseApiService simpleCaseApiService;

    @Resource
    private ISimpleCaseService simpleCaseService;


    /**
     * 描述： 校验单位配置
     *
     * @author: zhengqiang
     * @date: 2022/7/21
     * @param: orgCode
     * @return {@link Result}
     **/
    @GetMapping(value = "/checkSysDepartParam")
    public Result checkSysDepartParam(@RequestParam String orgCode) {
        if (simpleCaseApiService.checkSysDepartParam(orgCode)) {
            return Result.success("success");
        }
        return Result.error("fail");
    }

    /**
     * 描述: 简易案件保存方法
     *
     * @author: zhengqiang
     * @date: 2022/6/15
     * @param: saveDto
     * @return {@link Result< SimpleCaseSaveVo>}
     **/
    @PostMapping(value = "/saveSimpleCaseInfo")
    public Result<SimpleCaseSaveVo> saveSimpleCaseInfo(@RequestBody @Validated SimpleCaseSaveDto saveDto) {
        SimpleCaseSaveVo simpleCaseSaveVo = simpleCaseApiService.saveSimpleCaseInfo(saveDto);
        if (ObjectUtil.isNotEmpty(simpleCaseSaveVo)) {
            //生成当场处罚决定书及现场照片证据的文书
            try {
                simpleCaseApiService.dealSimpleCaseDocumentRecords(simpleCaseSaveVo.getCaseId());
                //设置案件为结案
                SimpleCaseSaveFinishDto simpleCaseSaveFinishDto = new SimpleCaseSaveFinishDto();
                simpleCaseSaveFinishDto.setCaseId(saveDto.getCaseId());
                simpleCaseService.saveCaseFinish(simpleCaseSaveFinishDto);

                return Result.success(simpleCaseSaveVo);
            } catch (Exception e) {
                e.printStackTrace();
                //生成文书异常,回滚案件保存数据
                if (simpleCaseService.deleteCaseInfo(simpleCaseSaveVo.getCaseId())) {
                    System.out.println("数据回滚成功!");
                }
                return Result.error("立案失败！" + e.getMessage());
            }
        }
        return Result.error("立案失败！");
    }

    /**
     * 描述： 生成简易处罚相关文书
     *
     * @author: zhengqiang
     * @date: 2022/7/20
     * @param: caseId
     * @return {@link Result< List< SimpleCaseDocVo>>}
     **/
    @GetMapping(value = "/saveSimpleCaseDoc")
    public Result<List<SimpleCaseDocVo>> saveSimpleCaseDoc(@RequestParam String caseId) {
        List<SimpleCaseDocVo> docList = simpleCaseApiService.saveSimpleCaseDoc(caseId);
        return Result.success(docList);
    }

    /**
     * 【V1.4.0】描述： 简易处罚暂存功能
     *
     * @author: zhengqiang
     * @date: 2022/8/15
     * @param: saveDto
     * @return {@link Result}
     **/
    @PostMapping("/saveSimpleCaseTemporary")
    public Result saveSimpleCaseTemporary(@RequestBody SimpleCaseSaveOfAppDto saveDto) {
        int count = simpleCaseApiService.saveSimpleCaseTemporary(saveDto);
        if (count > 0) {
            return Result.success("暂存成功");
        }
        return Result.error("暂存失败！");
    }

}
